//
//  MyOpinionListController.m
//  Stock
//
//  Created by sun black on 11-10-16.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "OpinionListController.h"
#import "StockListController.h"
#import "MyStock.h"
#import "JWRateStarView.h"

@implementation OpinionListController
@synthesize tableView=_tableView;
@synthesize service;
@synthesize centerTextLabel;
@synthesize rightTextLabel;



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.service = [[[StockAppService alloc] initWithDelegate:self] autorelease];
    [super viewDidLoad];
    if ([key isEqualToString:@"weekgrowth"]) {
        centerTextLabel.text = @"热度排名";
        rightTextLabel.text = @"排名变化";
    }
    
}


-(void)queryStocks{
    [super queryStocks];
    [service queryOpinionPool];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    [self.service cancleAllRequest];
    self.service = nil;
    self.key = nil;
    self.centerTextLabel=nil;
    self.rightTextLabel = nil;
}
-(void)dealloc{
    self.tableView = nil;
    [self.service cancleAllRequest];
    self.service = nil;
    self.key = nil;
    self.centerTextLabel=nil;
    self.rightTextLabel = nil;
    [super dealloc];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [quotationStocks count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        UIViewController *controller = [[UIViewController alloc] initWithNibName:@"OpinionListCell" bundle:nil];
        [cell.contentView addSubview:controller.view];
        [controller release];
    }    
    
    NSInteger index = [indexPath row];
    NSString *imageName = @"high_cell2.png";
    if (index%2==1) {
        imageName = @"high_cell1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:1];
    JWRateStarView *starView2 = (JWRateStarView *)[cell viewWithTag:2];
    JWRateStarView *starView3 = (JWRateStarView *)[cell viewWithTag:3];
    JWRateStarView *starView4 = (JWRateStarView *)[cell viewWithTag:4];
    JWRateStarView *starView5 = (JWRateStarView *)[cell viewWithTag:5];
    starView2.totalStarCount=5;
    starView3.totalStarCount=5;
    starView4.totalStarCount=5;
    starView5.totalStarCount=5;
    
    NSDictionary *dict = [quotationStocks objectAtIndex:indexPath.row];
    titleLabel.text = [dict objectForKey:@"name"];
    [starView2 setRate:[[dict objectForKey:@"totalScore"] floatValue]];
    [starView3 setRate:[[dict objectForKey:@"opinionScore"] floatValue]];
    [starView4 setRate:[[dict objectForKey:@"fundScore"] floatValue]];
    [starView5 setRate:[[dict objectForKey:@"priceScore"] floatValue]];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = [quotationStocks objectAtIndex:indexPath.row];
    
    MyStock *myStock = [[MyStock alloc] init];
    myStock.name = [dict objectForKey:@"name"];
    myStock.uuid = [dict objectForKey:@"id"];
    
    [StockContext shareInstance].remoteMyStocks = [NSMutableArray arrayWithObject:myStock];
    
    StockListController *stockListController = [[StockListController alloc] initWithNibName:@"StockListController" bundle:nil];
    stockListController.myStock = myStock;
    StockAppDelegate *delegate = (StockAppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.navigationController pushViewController:stockListController animated:YES];
    [stockListController release];
    
    [myStock release];
}



@end
